#ifndef TICTACTOE_H
#define TICTACTOE_H

#include <QWidget>
#include <QGridLayout>
#include <QPushButton>
#include <QVector>
#include <QLabel>

class TicTacToe : public QWidget
{
    Q_OBJECT

public:
    TicTacToe(QWidget *parent = nullptr);
    void updateBoard(const QVector<QVector<int>>& boardState);
    QPair<int, int> getScores() const;
    int isPlayerMove(const QVector<QVector<int>>& previousBoard, const QVector<QVector<int>>& currentBoard);
    int getGameCount() const;
    bool getplayerTurn();
    int findBestMove_normal(const QVector<QVector<int>>& boardState);
    int findBestMove_master(const QVector<QVector<int>>& boardState);
    int findBestMove(const QVector<QVector<int>>& boardState);
    void parseResponseToBoardState_byAPI(const QString& response);
    const QVector<QVector<int>>& printAndGetBoardState();
    void resetBoard();
    void printBoard(const QVector<QVector<int>>& boardState);
	
	bool Normal_Master_Flag = false;
    int whoWin = 0;

private slots:
    void onTileClicked();

private:
    QGridLayout *gridLayout;
    QVector<QPushButton*> tiles;
    bool playerTurn; // true for X人, false for O电脑
    bool enableComputerOpponent;//开启人机对弈，默认关闭
    int moves;
    int playerScore;
    int computerScore;
    QPushButton *normalButton;
    QPushButton *masterButton;
    QLabel *playerScoreLabel;
    QLabel *computerScoreLabel;
    QLabel *gameCountLabel;

    QVector<QVector<int>> boardState_byAPI;

    int GameCount = 0;

    void setupBoard();
    void checkWin();
    void updateScoreLabels();
};

#endif // TICTACTOE_H
